#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int p[N];

int find(int x) {
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}

void solve() {
    int n, m1, m2;
    cin >> n >> m1 >> m2;
    for(int i = 1; i <= n; i++) p[i] = i;
    vector<int> a1(m1), b1(m1), a2(m2), b2(m2);
    for(int i = 0; i < m1; i++){
        int x, y;
        cin >> x >> y;
        a1[i] = x, b1[i] = y;
    } 
    for(int i = 0; i < m2; i++){
        int x, y;
        cin >> x >> y;
        p[find(x)] = find(y);
        a2[i] = x, b2[i] = y;
    } 
    int ans = 0;
    vector<int> a, b;
    // 删掉边
    for(int i = 0; i < m1; i++) {
        if(find(a1[i]) != find(b1[i])) ans++;
        else a.push_back(a1[i]), b.push_back(b1[i]);
    }
    for(int i = 1; i <= n; i++) p[i] = i;
    for(int i = 0; i < a.size(); i++) {
        p[find(a[i])] = find(b[i]);
    }
    // 增加边
    for(int i = 0; i < m2; i++) {
        if(p[find(a2[i])] != p[find(b2[i])]) {
            p[find(a2[i])] = find(b2[i]);
            ans++;
        }
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}